//
//  UIWebViewVC.m
//  InterceptUrl
//
//  Created by WSXiong on 2017/10/11.
//  Copyright © 2017年 CMCC_CMSZ. All rights reserved.
//

#import "UIWebViewVC.h"


@interface UIWebViewVC ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *myWebView;

@property (nonatomic,strong) UIView *navBGView;

@end

@implementation UIWebViewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupNavBar];

    [self initMyWebView];
}

- (void)setupNavBar{
    self.navBGView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    self.navBGView.backgroundColor = [UIColor colorWithRed:230/256.0 green:230/256.0 blue:230/256.0 alpha:1];
    [self.view addSubview:self.navBGView];
    
    UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    returnBtn.frame = CGRectMake(0, 20, 80, 44);
    [returnBtn setTitle:@"<<返回" forState:UIControlStateNormal];
    [returnBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    returnBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.navBGView addSubview:returnBtn];
    [returnBtn addTarget:self action:@selector(returnBtn:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)returnBtn:(UIButton *)btn{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)initMyWebView{
    self.myWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64)];
    [self.view addSubview:_myWebView];
    self.myWebView.delegate = self;
//    NSLog(@"--%@",_inputUrlStr);
    [self loadWithUrlStr:_inputUrlStr];
    
    //获取本地
//    NSString *pathStr = [[NSBundle mainBundle] pathForResource:@"alipayios.html" ofType:nil];
//    [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:pathStr]]];
}

- (void)loadWithUrlStr:(NSString*)urlStr
{
    if (urlStr.length > 0) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSURLRequest *webRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]
                                                        cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                    timeoutInterval:30];
            [self.myWebView loadRequest:webRequest];
        });
    }
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *reqUrl = request.URL.absoluteString;
    if ([reqUrl hasPrefix:@"alipays://"] || [reqUrl hasPrefix:@"alipay://"] || [reqUrl hasPrefix:@"weixin://"]) {
    // NOTE: 跳转支付App
        BOOL bSucc = [[UIApplication sharedApplication] canOpenURL:request.URL];
             if (bSucc) {
            //TODO ANYTHING
         UIApplication *application = [UIApplication sharedApplication];
         if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
             [application openURL:request.URL options:@{} completionHandler:nil];
         }else{
             [application openURL:request.URL];
         }
     }else{
         // NOTE: 如果跳转失败，则跳转到itune下载支付app

     }
         return NO;
    }
     return YES;
}

- (void)dealloc{
    self.myWebView.delegate = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
